home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / UNIX / C / INDENT / INDNTGLB.H < prev    next >
C/C++ Source or Header  |  1989-09-03  |  16KB  |  418 lines

  1. /*
  2.  * Copyright (c) 1985 Sun Microsystems, Inc.
  3.  * Copyright (c) 1980 The Regents of the University of California.
  4.  * Copyright (c) 1976 Board of Trustees of the University of Illinois.
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms are permitted
  8.  * provided that the above copyright notice and this paragraph are
  9.  * duplicated in all such forms and that any documentation,
  10.  * advertising materials, and other materials related to such
  11.  * distribution and use acknowledge that the software was developed
  12.  * by the University of California, Berkeley, the University of Illinois,
  13.  * Urbana, and Sun Microsystems, Inc.  The name of either University
  14.  * or Sun Microsystems may not be used to endorse or promote products
  15.  * derived from this software without specific prior written permission.
  16.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
  17.  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
  18.  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19.  *
  20.  *    @(#)indent_globs.h    5.7 (Berkeley) 9/15/88
  21.  */
  22.  
  23. #include <stdio.h>
  24. #include <strings.h>
  25.  
  26. /* Standard memory allocation routines.  */
  27. char *malloc ();
  28. char *realloc ();
  29. /* Do the same thing, but abort with an error if out of memory
  30.    (see globs.c).  */
  31. char *xmalloc ();
  32. char *xrealloc ();
  33.  
  34. /* Because some systems lack memcpy, I have provided one, called
  35.    mymemcpy.  If you system memcpy is more efficient, you might
  36.    want to use it by uncommenting the following line.  */
  37. /* #define mymemcpy memcpy */
  38. char *mymemcpy ();
  39.  
  40. #define BACKSLASH '\\'
  41. #define label_offset 2        /* number of levels a label is placed to left
  42.                  * of code */
  43. /* Initial size of internal buffers (they are extended as necessary).  */
  44. #define bufsize 1000
  45.  
  46. #define tabsize 8        /* the size of a tab */
  47. #define tabmask 0177770        /* mask used when figuring length of lines
  48.                  * with tabs */
  49.  
  50. enum codes {code_eof = 0,  /* end of file */
  51.           newline,
  52.           lparen, /* '(' or '['.  Also '{' in an initialization.  */
  53.           rparen, /* ')' or ']'.  Also '}' in an initialization.  */
  54.           unary_op, binary_op, postop,
  55.           question, casestmt, colon, semicolon, lbrace, rbrace,
  56.           ident, /* string or char literal, identifier, number */
  57.           comma, comment, swstmt,
  58.           preesc,  /* '#'.  */
  59.           form_feed, decl,
  60.           sp_paren, /* if, for, or while token */
  61.           sp_nparen, ifstmt, whilestmt,
  62.           forstmt, stmt, stmtl, elselit, dolit, dohead, ifhead,
  63.           elsehead, period };
  64.           
  65. #define false 0
  66. #define true  1
  67.  
  68. /* Name of input file.  */
  69. extern char *in_name;
  70.  
  71. char *in_prog; /* pointer to the null-terminated input program */
  72.  
  73. /* Point to the position in the input program which we are currently
  74.    looking at.  */
  75. char *in_prog_pos;
  76.  
  77. /* Point to the start of the current line.  */
  78. char *cur_line;
  79.  
  80. /* Size of the input program, not including the ' \n\0' we add at the end */
  81. int in_prog_size;
  82.  
  83. FILE       *output;        /* the output file */
  84.  
  85. #define check_code_size \
  86.     if (e_code >= l_code) { \
  87.         register nsize = l_code-s_code+400; \
  88.         codebuf = (char *) realloc(codebuf, nsize); \
  89.         e_code = codebuf + (e_code-s_code) + 1; \
  90.         l_code = codebuf + nsize - 5; \
  91.         s_code = codebuf + 1; \
  92.     }
  93.  
  94. #define check_com_size \
  95.     if (e_com >= l_com) { \
  96.         register nsize = l_com-s_com+400; \
  97.         combuf = (char *) realloc(combuf, nsize); \
  98.         e_com = combuf + (e_com-s_com) + 1; \
  99.         l_com = combuf + nsize - 5; \
  100.         s_com = combuf + 1; \
  101.     }
  102.  
  103. #define check_lab_size \
  104.     if (e_lab >= l_lab) { \
  105.         register nsize = l_lab-s_lab+400; \
  106.         labbuf = (char *) realloc(labbuf, nsize); \
  107.         e_lab = labbuf + (e_lab-s_lab) + 1; \
  108.         l_lab = labbuf + nsize - 5; \
  109.         s_lab = labbuf + 1; \
  110.     }
  111.  
  112. char       *labbuf;        /* buffer for label */
  113. char       *s_lab;        /* start ... */
  114. char       *e_lab;        /* .. and end of stored label */
  115. char       *l_lab;        /* limit of label buffer */
  116.  
  117. char       *codebuf;        /* buffer for code section */
  118. char       *s_code;        /* start ... */
  119. char       *e_code;        /* .. and end of stored code */
  120. char       *l_code;        /* limit of code section */
  121.  
  122. char       *combuf;        /* buffer for comments */
  123. char       *s_com;        /* start ... */
  124. char       *e_com;        /* ... and end of stored comments */
  125. char       *l_com;        /* limit of comment buffer */
  126.  
  127. char       *buf_ptr;        /* ptr to next character to be taken from
  128.                  * in_buffer */
  129. char       *buf_end;        /* ptr to first after last char in in_buffer */
  130.  
  131. /* pointer to the token that lexi() has just found */
  132. char *token;
  133. /* points to the first char after the end of token */
  134. char *token_end;
  135. /* Functions from lexi.c */
  136. enum codes lexi ();
  137.  
  138. /* Used to keep track of buffers.  */
  139. struct buf {
  140.   char *ptr;  /* points to the start of the buffer */
  141.   char *end;  /* points to the character beyond the last one (e.g. is equal
  142.          to ptr if the buffer is empty).  */
  143.   int size;  /* how many chars are currently allocated.  */
  144. };
  145. /* Insure that BUFSTRUC has at least REQ more chars left, if not extend it.
  146.    Note:  This may change bufstruc.ptr.  */
  147. #define need_chars(bufstruc, req) \
  148.   if ((bufstruc.end - bufstruc.ptr + (req)) >= bufstruc.size) \
  149.     {\
  150.       int cur_chars = bufstruc.end - bufstruc.ptr;\
  151.       bufstruc.size *= 2;\
  152.       bufstruc.ptr = xrealloc(bufstruc.ptr,bufstruc.size);\
  153.       bufstruc.end = bufstruc.ptr + cur_chars;\
  154.     }
  155. /* Initialize BUFSTRUC.  */
  156. #define init_buf(bufstruc) \
  157.   bufstruc.end = bufstruc.ptr = xmalloc(bufsize),\
  158.   bufstruc.size = bufsize
  159.  
  160. /* Buffer in which to save a comment which occurs between an if(), while(),
  161.    etc., and the statement following it.  Note: the fact that we point
  162.    into this buffer, and that we might realloc() it (via the
  163.    need_chars macro) is a bad thing (since when the buffer is
  164.    realloc'd its address might change, making any pointers into it
  165.    point to garbage), but since the filling of the buffer (hence the
  166.    need_chars) and the using of the buffer (where buf_ptr points into
  167.    it) occur at different times, we can get away with it (it would not
  168.    be trivial to fix).  */
  169. struct buf save_com;
  170.  
  171. char       *bp_save;        /* saved value of buf_ptr when taking input
  172.                  * from save_com */
  173. char       *be_save;        /* similarly saved value of buf_end */
  174.  
  175.  
  176. int         pointer_as_binop;
  177. int         blanklines_after_declarations;
  178. int         blanklines_before_blockcomments;
  179. extern int blanklines_after_procs;
  180. int         blanklines_around_conditional_compilation;
  181. int         swallow_optional_blanklines;
  182. int         n_real_blanklines;
  183. int         prefix_blankline_requested;
  184. int         postfix_blankline_requested;
  185. int         break_comma;    /* when true and not in parens, break after a
  186.                  * comma */
  187.  
  188. /* number of spaces to indent braces from the suround if, while, etc.
  189.    in -bl (bype_2 == 0) code */
  190. int brace_indent;
  191.  
  192. int         btype_2;        /* when true, brace should be on same line as
  193.                  * if, while, etc */
  194. /* If true, a space is inserted between if, while, or for, and a semicolon
  195.    for example
  196.    while (*p++ == ' ') ;
  197.    */
  198. int space_sp_semicolon;
  199.  
  200. /* True if a #else or #endif has been encountered.  */
  201. extern int else_or_endif;
  202.  
  203. int         case_ind;        /* indentation level to be used for a "case
  204.                  * n:" in spaces */
  205. int         code_lines;        /* count of lines with code */
  206. int         had_eof;        /* set to true when input is exhausted */
  207. int         line_no;        /* the current line number. */
  208. int         max_col;        /* the maximum allowable line length */
  209. int         verbose;        /* when true, non-essential error messages are
  210.                  * printed */
  211. int         cuddle_else;    /* true if else should cuddle up to '}' */
  212. int         star_comment_cont;    /* true iff comment continuation lines should
  213.                  * have stars at the beginning of each line. */
  214. int         comment_delimiter_on_blankline;
  215. int         troff;        /* true iff were generating troff input */
  216. int         procnames_start_line;    /* if true, the names of procedures
  217.                      * being defined get placed in column
  218.                      * 1 (ie. a newline is placed between
  219.                      * the type of the procedure and its
  220.                      * name) */
  221. int         proc_calls_space;    /* If true, procedure calls look like:
  222.                  * foo (bar) rather than foo(bar) */
  223. int        cast_space;        /* If true, casts look like:
  224.                  * (char *) bar rather than (char *)bar */
  225.  
  226. /* If comments which start in column 1 are to be magically reformatted */
  227. int         format_col1_comments;
  228. /* If any comments are to be reformatted */
  229. int format_comments;
  230.  
  231. extern int  suppress_blanklines;/* set iff following blanklines should be
  232.                  * suppressed */
  233. int         continuation_indent;/* set to the indentation between the edge of
  234.                  * code and continuation lines in spaces */
  235. int         lineup_to_parens;    /* if true, continued code within parens will
  236.                  * be lined up to the open paren */
  237.  
  238. /* The position that we will line the current line up with when it
  239.    comes time to print it (if we are lining up to parentheses).  */
  240. extern int paren_target;
  241.  
  242. int         Bill_Shannon;    /* true iff a blank should always be inserted
  243.                  * after sizeof */
  244. int         blanklines_after_declarations_at_proctop;    /* This is vaguely
  245.                              * similar to
  246.                              * blanklines_after_decla
  247.                              * rations except that
  248.                              * it only applies to
  249.                              * the first set of
  250.                              * declarations in a
  251.                              * procedure (just after
  252.                              * the first '{') and it
  253.                              * causes a blank line
  254.                              * to be generated even
  255.                              * if there are no
  256.                              * declarations */
  257. int         block_comment_max_col;
  258. int         extra_expression_indent;    /* True if continuation lines from the
  259.                      * expression part of "if(e)",
  260.                      * "while(e)", "for(e;e;e)" should be
  261.                      * indented an extra tab stop so that
  262.                      * they don't conflict with the code
  263.                      * that follows */
  264.  
  265. /* The following are all controlled by command line switches
  266.    (as are some of the things above).  */
  267. extern int         leave_comma;    /* if true, never break declarations after
  268.                  * commas */
  269. extern int         decl_com_ind;    /* the column in which comments after
  270.                  * declarations should be put */
  271. extern int         case_indent;    /* The distance to indent case labels from the
  272.                  * switch statement */
  273. extern int         com_ind;    /* the column in which comments to the right
  274.                  * of code should start */
  275. extern int         decl_indent;    /* column to indent declared identifiers to */
  276. extern int         ljust_decl;    /* true if declarations should be left
  277.                  * justified */
  278. extern int         unindent_displace;    /* comments not to the right of code
  279.                      * will be placed this many
  280.                      * indentation levels to the left of
  281.                      * code */
  282. extern int         else_if;    /* True iff else if pairs should be handled
  283.                  * specially */
  284. /* Number of spaces to indent parameters.  */
  285. extern int         indent_parameters;
  286. /* The size of one indentation level in spaces.  */
  287. extern int         ind_size;
  288. /* Nonzero if we should use standard input/output when files are not
  289.    explicitly specified.  */
  290. extern int         use_stdinout;
  291.  
  292. /* -troff font state information */
  293.  
  294. struct fstate {
  295.     char        font[4];
  296.     char        size;
  297.     int         allcaps:1;
  298. };
  299. char       *chfont();
  300.  
  301. struct fstate
  302.             keywordf,        /* keyword font */
  303.             stringf,        /* string font */
  304.             boxcomf,        /* Box comment font */
  305.             blkcomf,        /* Block comment font */
  306.             scomf,        /* Same line comment font */
  307.             bodyf;        /* major body font */
  308.  
  309. /* Initial size for the parser's stacks.  (p_stack, il, and cstk).  */
  310. #define INITIAL_STACK_SIZE 2
  311.  
  312. struct parser_state {
  313.     struct parser_state *next;
  314.     enum codes last_token;
  315.     struct fstate cfont;    /* Current font */
  316.  
  317.     /* This is the parsers stack, and the current allocated size.  */
  318.     enum codes *p_stack;
  319.     int p_stack_size;
  320.  
  321.     /* This stack stores indentation levels */
  322.     /* Currently allocated size is stored in p_stack_size.  */
  323.     int *il;
  324.  
  325.     /* Used to store case stmt indentation levels.  */
  326.     /* Currently allocated size is stored in p_stack_size.  */
  327.     int *cstk;
  328.  
  329.     /* Pointer to the top of stack of the p_stack, il and cstk arrays. */
  330.     int         tos;
  331.     
  332.     int         box_com;    /* set to true when we are in a "boxed"
  333.                  * comment. In that case, the first non-blank
  334.                  * char should be lined up with the / in /* */
  335.     int         comment_delta,
  336.                 n_comment_delta;
  337.     int         cast_mask;    /* indicates which close parens close off
  338.                  * casts */
  339.     int         sizeof_mask;    /* indicates which close parens close off
  340.                  * sizeof''s */
  341.     int         block_init;    /* true iff inside a block initialization */
  342.     int         block_init_level;    /* The level of brace nesting in an
  343.                      * initialization */
  344.     int         last_nl;    /* this is true if the last thing scanned was
  345.                  * a newline */
  346.     int         in_or_st;    /* Will be true iff there has been a
  347.                  * declarator (e.g. int or char) and no left
  348.                  * paren since the last semicolon. When true,
  349.                  * a '{' is starting a structure definition or
  350.                  * an initialization list */
  351.     int         bl_line;    /* set to 1 by dump_line if the line is blank */
  352.     int         col_1;        /* set to true if the last token started in
  353.                  * column 1 */
  354.     int         com_col;    /* this is the column in which the current
  355.                  * coment should start */
  356.     int         com_lines;    /* the number of lines with comments, set by
  357.                  * dump_line */
  358.     int         dec_nest;    /* current nesting level for structure or init */
  359.     int         decl_on_line;    /* set to true if this line of code has part
  360.                  * of a declaration on it */
  361.     int         i_l_follow;    /* the level in spaces to which ind_level should be set
  362.                  * after the current line is printed */
  363.     int         in_decl;    /* set to true when we are in a declaration
  364.                  * stmt.  The processing of braces is then
  365.                  * slightly different */
  366.     int         in_stmt;    /* set to 1 while in a stmt */
  367.     int         ind_level;    /* the current indentation level in spaces */
  368.     int         ind_stmt;    /* set to 1 if next line should have an extra
  369.                  * indentation level because we are in the
  370.                  * middle of a stmt */
  371.     int         last_u_d;    /* set to true after scanning a token which
  372.                  * forces a following operator to be unary */
  373.     int         out_coms;    /* the number of comments processed, set by
  374.                  * pr_comment */
  375.     int         out_lines;    /* the number of lines written, set by
  376.                  * dump_line */
  377.     int         p_l_follow;    /* used to remember how to indent following
  378.                  * statement */
  379.     int         paren_level;    /* parenthesization level. used to indent
  380.                  * within stmts */
  381.     /* Column positions of paren at each level.  If positive, it
  382.        contains just the number of characters of code on the line up to
  383.        and including the right parenthesis character.  If negative, it
  384.        contains the opposite of the actual level of indentation in
  385.        characters (that is, the indentation of the line has been added
  386.        to the number of characters and the sign has been reversed to
  387.        indicate that this has been done).  */
  388.     short *paren_indents;    /* column positions of each paren */
  389.     int paren_indents_size;  /* Currently allocated size.  */
  390.  
  391.     int         pcase;        /* set to 1 if the current line label is a
  392.                  * case.  It is printed differently from a
  393.                  * regular label */
  394.     int         search_brace;    /* set to true by parse when it is necessary
  395.                  * to buffer up all info up to the start of a
  396.                  * stmt after an if, while, etc */
  397.     int         use_ff;        /* set to one if the current line should be
  398.                  * terminated with a form feed */
  399.     int         want_blank;    /* set to true when the following token should
  400.                  * be prefixed by a blank. (Said prefixing is
  401.                  * ignored in some cases.) */
  402.     int         its_a_keyword;
  403.     int         sizeof_keyword;
  404.     int         dumped_decl_indent;
  405.     int         in_parameter_declaration;
  406.     char *procname;    /* The name of the current procedure */
  407.     char *procname_end;  /* One char past the last one in procname */
  408.     int         just_saw_decl;
  409. };
  410. /* All manipulations of the parser stack occur at the tos
  411.    (via the macro ps).  The elements of the stack below it are kept in
  412.    a linked list via the next field.  */
  413. extern struct parser_state *parser_state_tos;
  414.  
  415. /* The column in which comments to the right of #else and #endif should
  416.    start.  */
  417. extern int else_endif_col;
  418.